<?php

/**
 * controlLlamada actions.
 *
 * @package    cronos-doctrine
 * @subpackage controlLlamada
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class controlLlamadaActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->control_llamadas = Doctrine_Core::getTable('ControlLlamada')
      ->createQuery('a');
    $this->pager = new sfDoctrinePager('ControlLlamada', sfConfig::get('app_cliente_x_pag'));
    $this->pager->setQuery($this->control_llamadas);
    $this->pager->setPage($this->getUser()->getAttribute('page'));
    $this->pager->init();
    
    $this->idUser =  $request->getParameter('idUser');
    $this->parent =  $request->getParameter('parent');
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new ControlLlamadaForm();
    
      
    $agente_id = $this->getUser()->getAttribute('agente_user');
    $this->form->setWidget('agente_id', new sfWidgetFormInputHidden(array(),array('value' => $agente_id)));
     //Reasigna la variable usuario para su actualizacion luego Jquery
    $this->idUser = $request->getParameter('idUser');
    $this->parent =  $request->getParameter('parent');
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new ControlLlamadaForm();
    $agente_id = $this->getUser()->getAttribute('agente_user');
    $this->form->setWidget('agente_id', new sfWidgetFormInputHidden(array(),array('value' => $agente_id)));
    
    //Captura Variable Id Usuario
    $this->idUser =  $request->getParameter('idUser');
    $this->parent =  $request->getParameter('parent');    
    $this->processForm($request, $this->form, $this->idUser,$this->parent);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($control_llamada = Doctrine_Core::getTable('ControlLlamada')->find(array($request->getParameter('id'))), sprintf('Object control_llamada does not exist (%s).', $request->getParameter('id')));
    $this->idUser =  $request->getParameter('idUser');
    $this->parent =  $request->getParameter('parent');
    $this->form = new ControlLlamadaForm($control_llamada);
      
    $agente_id = $this->getUser()->getAttribute('agente_user');
    $this->form->setWidget('agente_id', new sfWidgetFormInputHidden(array(),array('value' => $agente_id)));
    
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($control_llamada = Doctrine_Core::getTable('ControlLlamada')->find(array($request->getParameter('id'))), sprintf('Object control_llamada does not exist (%s).', $request->getParameter('id')));
    $this->form = new ControlLlamadaForm($control_llamada);
    $agente_id = $this->getUser()->getAttribute('agente_user');
    $this->form->setWidget('agente_id', new sfWidgetFormInputHidden(array(),array('value' => $agente_id)));
    //Captura Variable Id Usuario
    $this->idUser =  $request->getParameter('idUser');
    $this->parent =  $request->getParameter('parent');
    $this->processForm($request, $this->form, $this->idUser,$this->parent);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($control_llamada = Doctrine_Core::getTable('ControlLlamada')->find(array($request->getParameter('id'))), sprintf('Object control_llamada does not exist (%s).', $request->getParameter('id')));
    $control_llamada->delete();

    $this->redirect('controlLlamada/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form, $idUser, $parent)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $control_llamada = $form->save();
      $this->redirect('controlLlamada/index');
      
    }
  }
}
